package CutLoad.components.container
{
	import CutLoad.Base;
	import CutLoad.cookies.Fun;
	import CutLoad.cookies.Painter;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 通过视窗框，查看一个对象 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class ContainerView extends Base
	{
		private var viewMask:Sprite;
		private var speed:Number;
		private var viewRect:Rectangle;
		private var body:Base;
		private var movePoint:Point
		//距离小于这些就说明到边界
		private var offsetW:Number,offsetH:Number;
		//
		private var ChangPosition:Function=null;
		
		/**
		 *  
		 * @param body 背景,要显示的对象
		 * @param viewRect 视角窗口大小
		 * @param speed 移动速度
		 * 
		 */		
		public function ContainerView(body:Base,viewRect:Rectangle,speed:Number=2)
		{
			if(body.getShowWidth()<viewRect.width) throw new Error("视角窗口太宽!");
			if(body.getShowHeight()<viewRect.height) throw new Error("视角窗口太高!");
			this.speed=speed;
			this.viewRect=viewRect;
			//
			this.body=body;
			body.setXY(0,0,this);
			viewMask=Painter.getRect2(viewRect,0xffffff,0.5);
			this.addChild(viewMask);
			body.mask=viewMask;
			//
			offsetW=body.width-Math.floor(body.getShowWidth()/speed)*speed;
			offsetH=body.height-Math.floor(body.getShowHeight()/speed)*speed;
			//
			setMovePoint(new Point());
		}
		/**
		 * 设置当移视图时，需要调用的方法 
		 * @param fun 格式:function(本对象:ContainerView)
		 * 
		 */		
		public function setFunChangPosition(fun:Function):void{
			ChangPosition=fun;
		}
		/**
		 * 返回视窗信息 
		 * @return 
		 * 
		 */		
		public function getViewRect():Rectangle{
			return viewRect;
		}
		/**
		 * 移动视窗 
		 * @param x
		 * @param y
		 * 
		 */		
		public function move(x:Number,y:Number):void{
			var wileP:Point=new Point(x,y);
			if(!containsPoint(wileP)){
				return;
			}
			var xx:Number=wileP.x-movePoint.x;
			var yy:Number=wileP.y-movePoint.y;
			body.x-=xx;
			body.y-=yy;
			setMovePoint(wileP);
		}
		/**
		 * 取消或显示遮蔽 
		 * @param isOk false取消  true显示
		 * 
		 */		
		public function set EableMask(isOk:Boolean):void{
			if(isOk){
				body.mask=viewMask;
			}else{
				body.mask=null;
			}
		}
		/**
		 * 返回视窗目前的坐标 
		 * @return 
		 * 
		 */		
		public function getViewPosition():Point{
			return movePoint.clone()
		}
		/**
		 * 视角窗口的坐标是否在显示的范围内,注：坐标值为视解的左上角
		 * @param p
		 * @return 
		 * 
		 */		
		public function containsPoint(p:Point):Boolean{
			var checkRect:Rectangle=new Rectangle(p.x,p.y,viewRect.width,viewRect.height)
			var arr4:Array=Fun.rect4Point(checkRect);
			var rectBig:Rectangle=new Rectangle(0,0,body.getShowWidth(),body.getShowHeight());
			//
			for(var i:Number=0;i<arr4.length;i++){
				if(!Fun.rectContainsPoint(rectBig,arr4[i])){
					return false;
				}
			}
			return true; 
		}
		//设置当前所在的坐标
		private function setMovePoint(p:Point):void{
			movePoint=p.clone()
			if(ChangPosition!=null) ChangPosition(this);
		}
		/**
		 * 视角向上移动 
		 * 
		 */		
		public function up():void{
			var wileP:Point=new Point(movePoint.x,movePoint.y-this.speed);
			if(!containsPoint(wileP)){
				if(wileP.y<0){
					wileP=new Point(movePoint.x,0)
					body.y=0
					setMovePoint(wileP);
				}
				return;
			}else{
				body.y+=this.speed;
			}
			setMovePoint(wileP);
		}
		/**
		 * 视角向右移动 
		 * 
		 */
		public function right():void{
			var wileP:Point=new Point(movePoint.x+this.speed,movePoint.y);
			if(!containsPoint(wileP)){
				var dd:Number=Math.abs(movePoint.x-(body.getShowWidth()-viewRect.width))
				if(dd<=this.offsetW){
					wileP=new Point(movePoint.x+dd,movePoint.y);
					body.x=-body.getShowWidth()+viewRect.width;
					setMovePoint(wileP);
				}
				return;
			}else{
				body.x-=this.speed;
			}
			setMovePoint(wileP);
		}
		/**
		 * 视角向下移动 
		 * 
		 */
		public function down():void{
			var wileP:Point=new Point(movePoint.x,movePoint.y+this.speed);
			if(!containsPoint(wileP)){
				var dd:Number=Math.abs(movePoint.y-(body.getShowHeight()-viewRect.height))
				if(dd<=this.offsetH){
					wileP=new Point(movePoint.x,movePoint.y+dd)
					body.y=-body.getShowHeight()+viewRect.height
						setMovePoint(wileP);
				}
				return;
			}else{
				body.y-=this.speed;
			}
			setMovePoint(wileP);
		}
		/**
		 * 视角向左移动 
		 * 
		 */
		public function left():void{
			var wileP:Point=new Point(movePoint.x-this.speed,movePoint.y);
			if(!containsPoint(wileP)){
				if(wileP.x<0){
					wileP=new Point(0,movePoint.y)
					body.x=0
					setMovePoint(wileP);
				}
				return;
			}else{
				body.x+=this.speed;
			}
			setMovePoint(wileP);
		}
		override public function getShowWidth():Number{
			return viewRect.width;
		}
		override public function getShowHeight():Number{
			return viewRect.height;
		}
	}
}